<?php

namespace App\Service\Common;

use App\Service\FuYou\FuYouService;
use Exception;
use Illuminate\Support\Facades\Log;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PhpImap;

class EmailV2Service
{
    public string $emailHost, $emailPort, $contactEmail, $emailPassword, $sendUserName;

    public array $contactEmailCC;

    public function __construct($config)
    {
        $this->emailHost      = $config['host'];               //SMTP服务器
        $this->emailPort      = $config['port'];               //服务器端口 25 或者465 具体要看邮箱服务器支持
        $this->contactEmail   = $config['userName'];           //联系邮箱
        $this->emailPassword  = $config['password'];           //邮箱是授权码
        $this->sendUserName   = $config['sendUserName'];       //发件人名字
        $this->contactEmailCC = $config['contactEmailCc'];     //抄送邮箱
    }

    /**
     * 发送邮件
     * @param $receiveEmail
     * @param $receiveName
     * @param $title
     * @param $body
     * @param string $receiveCCEmail
     * @return array
     */
    public function sendEmail(
        string $receiveEmail,
        string $receiveName,
        string $title,
        string $body,
        array $otherFileData = []
    ): array {
        // Passing `true` enables exceptions
        $mail = new PHPMailer(true);
        $data = [true, ' 邮件发送成功'];
        try {
            $SMTPDebug = SMTP::DEBUG_OFF;
            //            if (config('app.env') == 'dev') {
            //                $SMTPDebug = SMTP::DEBUG_CONNECTION;// 调试模式输出
            //            }

            // 服务器配置
            $mail->CharSet   = "UTF-8";
            $mail->SMTPDebug = $SMTPDebug;
            $mail->isSMTP();
            $mail->Host       = $this->emailHost;
            $mail->SMTPAuth   = true;
            $mail->Username   = $this->contactEmail;
            $mail->Password   = $this->emailPassword;
            $mail->SMTPSecure = 'ssl';
            $mail->Port       = $this->emailPort;
            $mail->setFrom($this->contactEmail, $this->sendUserName);     //发件人
            $mail->addAddress($receiveEmail, $receiveName);               // 收件人
            $mail->addReplyTo($this->contactEmail, $this->sendUserName);  //回复的时候回复给哪个邮箱 建议和发件人一致

            // 抄送邮箱
            $receiveCCEmail = $this->contactEmailCC;
            if ($receiveCCEmail) {
                if (is_array($receiveCCEmail) && count($receiveCCEmail)) {
                    foreach ($receiveCCEmail as $v) {
                        $mail->addCC($v);
                    }
                } else {
                    $mail->addCC($receiveCCEmail);
                }
            }


            // 附件地址
            if ($otherFileData && is_array($otherFileData) && count($otherFileData)) {
                foreach ($otherFileData as $v) {
                    $mail->addAttachment($v['url'], $v['name']);
                }
            }

            // Content
            $mail->isHTML(true);                                          // 是否以HTML文档格式发送  发送后客户端可直接显示对应HTML内容
            $mail->Subject = $title;                                      //标题
            $mail->Body    = $body;                                       //正文
            $mail->send();
        } catch (Exception $e) {
            $data = [false, $title . ' 邮件发送失败: ', $mail->ErrorInfo];
        }
        Log::info(
            '发送邮件:',
            [
                'receiveEmail' => $receiveEmail,
                'receiveName'  => $receiveName,
                'title'        => $title,
                'body'         => $body,
                'res'          => $data,
            ]
        );
        return $data;
    }


}
